﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Polar.AI.Common
{
    public static class Vector2Extensions
    {
        /// <summary>
        /// Converts to a Vector3 using the X and Z coordinates
        /// </summary>
        public static Vector3 ToVector3(this Vector2 v)
        {
            return new Vector3(v.X, 0.0f, v.Y);
        }

        /// <summary>
        /// Gets the radian angle from vector reduced to a smaller precision.
        /// </summary>
        public static float ToRadians(this Vector2 v)
        {
            return (float)Math.Round((decimal)Math.Atan2(v.Y, v.X), 5);
        }


        /// <summary>
        /// Clamps the length of a given vector to maxLength.
        /// </summary>
        public static Vector2 TruncateLength(this Vector2 vector, float maxLength)
        {
            float maxLengthSquared = maxLength * maxLength;
            float vecLengthSquared = vector.LengthSquared();
            if (vecLengthSquared <= maxLengthSquared)
                return vector;
            else
                return (vector * (maxLength / (float)Math.Sqrt(vecLengthSquared)));
        }

        /// <summary>
        /// Returns is the vector is zero or NaN
        /// </summary>
        public static bool IsZero(this Vector2 vector)
        {
            if (vector == Vector2.Zero)
                return true;
            else if (float.IsNaN(vector.X) && float.IsNaN(vector.Y))
                return true;
            else
                return false;
        }
    }
}
